package stock.controller;

import org.elasticsearch.search.SearchHits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import stock.bean.Goods;
import stock.service.ElasticsearchService;
import stock.utils.Result;

import java.util.List;

@RestController()
@RequestMapping("es")
public class ElasticsearchController {

    @Autowired
    ElasticsearchService elasticsearchService;

    @GetMapping("/search")
    public Object search(@RequestParam(value = "keyword") String keyword){
        System.out.println("搜索接口");
        List<Goods> s = elasticsearchService.searchHits(keyword);
        return Result.success(s);
    }
    @GetMapping("/search/title")
    public Object searchTitle(@RequestParam(value = "keyword") String keyword){
        System.out.println("搜索接口");
        List<Goods> s = elasticsearchService.findByTitle(keyword);
        return Result.success(s);
    }

    @GetMapping("/search/titleOrPrice")
    public Object searchByTitleOrPrice(@RequestParam(value = "keyword") Object keyword){
        List<Goods> s = elasticsearchService.findByTitleOrPrice(keyword);
        return Result.success(s);
    }

    @GetMapping("/search/titleAndPrice")
    public Object searchByTitleAndPrice(@RequestParam(value = "keyword") Object keyword){
        List<Goods> s = elasticsearchService.findByTitleAndPrice(keyword);
        return Result.success(s);
    }

    @GetMapping("/search/titleOrCover")//?keyword=口
    public Object searchByTitleOrCover(@RequestParam(value = "keyword") String keyword){
        List<Goods> s = elasticsearchService.findByTitleOrCover(keyword);
        return Result.success(s);
    }

//    @GetMapping("/search/by/page")//?keyword=霜&page=1&size=2
//    public Object searchByPage(@RequestParam(value = "keyword") String keyword,
//                               @RequestParam(value = "page") Integer page,
//                               @RequestParam(value = "size") Integer size){
//        elasticsearchService.searchPage(keyword,page,size);
//        return Result.success();
//    }
}
